package com.imooc.o2o.util;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by PopQing on 2018/4/21.
 */
public class ImageUtil {
    private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final Random r = new Random();
    private static Logger logger = LoggerFactory.getLogger(ImageUtil.class);
    public static String generateThumbnail(CommonsMultipartFile thumbnail,String targetAddr){
        String realFileName = getRandomFileName();
        String extension = getFileExtension(thumbnail);
        makeDirPath(targetAddr);
        String relativeAddr = targetAddr + realFileName + extension;
        File dest = new File(PathUtil.getImgBasePath()+relativeAddr);
        try {
            Thumbnails.of(thumbnail.getInputStream()).size(200,200).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/checkphoto.bmp")),0.25f).outputQuality(0.8f).toFile("C:\\Users\\PopQing\\Desktop\\out.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return relativeAddr;
    }

    private static String getFileExtension(CommonsMultipartFile cFile) {
        String originalFileName =  cFile.getOriginalFilename();
        return originalFileName.substring(originalFileName.lastIndexOf("."));
    }

    public static String generateThumbnail(File thumbnail,String targetAddr){
        System.out.println("11111111111111111111111111111");
        String realFileName = getRandomFileName();
        System.out.println("22222222222222222222222222222");
        String extension = getFileExtension(thumbnail);
        System.out.println("33333333333333333333333333333");
        makeDirPath(targetAddr);
        System.out.println("44444444444444444444444444444");
        String relativeAddr = targetAddr + realFileName + extension;
        System.out.println("55555555555555555555555555555");
        logger.debug("current relativeAddr is:"+relativeAddr);
        File dest = new File(PathUtil.getImgBasePath()+relativeAddr);
        logger.debug("current complete addr is:"+PathUtil.getImgBasePath()+relativeAddr);
        try {
            Thumbnails.of(thumbnail).size(200,200).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/checkphoto.bmp")),0.25f).outputQuality(0.8f).toFile("C:\\Users\\PopQing\\Desktop\\out.png");
        } catch (IOException e) {
            logger.error(e.toString());
            e.printStackTrace();
        }
        return relativeAddr;
    }

    private static String getFileExtension(File cFile) {
        String originalFileName =  cFile.getName();
        return originalFileName.substring(originalFileName.lastIndexOf("."));
    }

    /**
     * 创建目标所涉及到的目录
     * @param targetAddr
     */
    private static void makeDirPath(String targetAddr) {
        String realFileParentPath = PathUtil.getImgBasePath()+targetAddr;
        File dirPath = new File(realFileParentPath);
        if(dirPath.exists()){
            dirPath.mkdirs();
        }
    }

    /**
     *
      * @return
     */
    private static String getRandomFileName() {
        int rannum = r.nextInt(89999)+10000;
        String nowTimeStr = sDateFormat.format(new Date());
        return nowTimeStr+rannum;
    }

    public static void main(String[] args) throws IOException {
        String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        Thumbnails.of(new File("C:\\Users\\PopQing\\Desktop\\1.png")).size(200,200).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath+"/checkphoto.bmp")),0.25f).outputQuality(0.8f).toFile("C:\\Users\\PopQing\\Desktop\\out.png");
    }
}
